package com.cssw.fyzb.busi.service.acct;


import com.cssw.fyzb.busi.util.ListToTreeUtil;
import com.cssw.fyzb.core.dao.acct.OutAcctDao;
import com.cssw.fyzb.core.dao.user.UserDao;
import com.cssw.fyzb.core.model.dodb.*;
import com.cssw.fyzb.core.model.vo.user.BankDistrictTreeVo;
import com.cssw.fyzb.core.util.BizUtil;
import com.ydn.dbframe.kit.StrKit;
import com.ydn.dbframe.plugin.activerecord.Db;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: yangyiqun
 * @Date: 2020/7/11 15:44
 */

@Service
public class OutAcctServiceImpl implements OutAcctService {

    @Autowired
    private OutAcctDao outAcctDao;

    @Autowired
    private UserDao userDao;

    @Override
    public UserWechat getWechatById(Long userId) {
        return outAcctDao.getWechatById(userId);
    }

    @Override
    public UserAli getAliById(Long userId) {
        return outAcctDao.getAliById(userId);
    }

    @Override
    public UserBank getBankByUserId(Long userId) {
        return outAcctDao.getBankByUserId(userId);
    }

    @Override
    public List<BankCode> listBankCode() {
        return outAcctDao.listBankCode();
    }

    @Override
    public List<BankCodeCh> listBankCodeCh() {
        return outAcctDao.listBankCodeCh();
    }

    @Override
    public List<UserBank> listBankById(Long userId) {
        return outAcctDao.listBankById(userId);
    }

    @Override
    public void deleteWechat(Long userId) {
        outAcctDao.deleteWechat(userId);
    }

    @Override
    public void deleteAli(Long userId) {
        outAcctDao.deleteAli(userId);
    }

    @Override
    public void deleteBank(Long userId, Long bankId) {
        UserBank userBank = outAcctDao.getBankById(bankId);
        if (null == userBank) {
            return;
        }

        if (!StrKit.equals(userId.toString(), userBank.getUserId().toString())) {
            return;
        }

        outAcctDao.deleteBank(bankId);
    }

    @Override
    public void saveAli(Long userId, String name, String aliNo, String receQrCode) {

        User user = userDao.getById(userId);
        if (!user.isFaceAuth()) {
            BizUtil.exception("您还未实名！");
        }

        if (!StrKit.equals(name, user.getUserName())) {
            BizUtil.exception("非实名认证的姓名！");
        }

        UserAli userAli = outAcctDao.getAliById(userId);
        if (null != userAli) {
            outAcctDao.updateAli(userId, name, aliNo, receQrCode);
            return;
        }

        outAcctDao.saveAli(userId, name, aliNo, receQrCode);
    }

    @Override
    public void saveWechat(Long userId, String name, String wechatNo, String receQrCode) {
        User user = userDao.getById(userId);
        if (!user.isFaceAuth()) {
            BizUtil.exception("您还未实名！");
        }

        if (!StrKit.equals(name, user.getUserName())) {
            BizUtil.exception("非实名认证的姓名！");
        }

        UserWechat userWechat = outAcctDao.getWechatById(userId);
        if (null != userWechat) {
            outAcctDao.updateWechat(userId, name, wechatNo, receQrCode);
            return;
        }

        outAcctDao.saveWechat(userId, name, wechatNo, receQrCode);
    }

    @Override
    public void saveBank(Long bankId, Long userId, String bankName, String cardNo, String openBank, String name, int defaultFlag,String province,String city,String bankCode) {
        User user = userDao.getById(userId);
        if (!user.isFaceAuth()) {
            BizUtil.exception("您还未实名！");
        }

//        if (!StrKit.equals(name, user.getUserName())) {
//            BizUtil.exception("非实名认证的姓名！");
//        }

        UserBank userBank = outAcctDao.getBankByIdAndCardNo(userId, cardNo);


        Db.tx(() -> {
            int newDefaultFlag = defaultFlag;
            if (UserBank.DEFUALT_FLAG_YES == newDefaultFlag) {
                UserBank defUserBank = outAcctDao.getBankByUserId(userId);
                if (null != defUserBank) {
                    //defUserBank.setDefaultFlag(UserBank.DEFUALT_FLAG_NO);
                    //outAcctDao.updateBank(defUserBank);
                    //取消默认
                    outAcctDao.updateNoDefaultFlgByUserId(userId);
                }
            } else {
                UserBank defUserBank = outAcctDao.getBankByUserId(userId);
                if (null == defUserBank){
                    newDefaultFlag = UserBank.DEFUALT_FLAG_YES;
                }
            }

            if (null == bankId) {
                if (null != userBank) {
                    BizUtil.exception("已经配置了该银行卡");
                }

                outAcctDao.saveBank(userId, bankName, cardNo, openBank, name, newDefaultFlag,province,city,bankCode);
            } else {
                if (null != userBank
                        && !StrKit.equals(bankId.toString(), userBank.getBankId().toString())) {
                    BizUtil.exception("已经配置了该银行卡");
                }

                outAcctDao.updateBank(bankId, userId, bankName, cardNo, openBank, name, newDefaultFlag,province,city,bankCode);
            }
            return true;
        });
    }

    @Override
    public void updateAli(Long userId, String name, String aliNo, String receQrCode) {
        UserAli userAli = outAcctDao.getAliById(userId);
        if (null == userAli) {
            return;
        }
        outAcctDao.updateAli(userId, name, aliNo, receQrCode);
    }

    @Override
    public void updateWechat(Long userId, String name, String wechatNo, String receQrCode) {
        UserWechat userWechat = outAcctDao.getWechatById(userId);
        if (null == userWechat) {
            return;
        }
        outAcctDao.updateWechat(userId, name, wechatNo, receQrCode);
    }

    @Override
    public void updateBank(Long bankId, Long userId, String bankName, String cardNo, String openBank, String name, int defaultFlag,String province,String city,String bankCode) {
        UserBank userBank = outAcctDao.getBankById(bankId);
        if (null == userBank) {
            return;
        }

        if (!StrKit.equals(userId.toString(), userBank.getUserId().toString())) {
            return;
        }

        Db.tx(() -> {
            if (UserBank.DEFUALT_FLAG_YES == defaultFlag) {
                //取消默认
                outAcctDao.updateNoDefaultFlgByUserId(userBank.getUserId());
            }
            outAcctDao.updateBank(bankId, userId, bankName, cardNo, openBank, name, defaultFlag,province,city,bankCode);

            return true;
        });

    }

    @Override
    public void updateDefaultFlag(Long bankId) {
        UserBank userBank = outAcctDao.getBankById(bankId);
        if (null == userBank){
            BizUtil.exception("信息不正确");
        }
        Db.tx(() -> {
            //取消默认
            outAcctDao.updateNoDefaultFlgByUserId(userBank.getUserId());
            //设置默认
            outAcctDao.defaultFlg(bankId);

            return true;
        });
    }

    @Override
    public List<BankDistrictTreeVo> getBankDistrictTree() {
        List<BankDistrict> bankDistricts =  outAcctDao.listBankDistrict();
        List<BankDistrictTreeVo> treeNodeVo = ListToTreeUtil.getBankDistrictTree(bankDistricts);
        return treeNodeVo;
    }
}